﻿using System.Linq;
using System.Web.Mvc;
using Century.Model;
using GKFX.Common.Business.BOL.Common;
using Telerik.Web.Mvc;
using Century.Web.Security.Attributes
using GKFX.Common.UI.MVC;
using GKFX.Business.Repository;

namespace Century.Web.MVC.AdminArea
{
    //[Kooboo.CMS.Web.Authorizations.Authorization(AreaName = "AdminTools", Group = "", Name = "SiteResources")]
    [Authorization(Group = "", Name = "SiteResources")]
    public class SiteResourcesController : CenturyAdminBaseController
    {
        //
        // GET: /AdminTools/SiteResources/

        CountrySiteResourceBSO db = new CountrySiteResourceBSO();
        ResourceTypeBSO typeDb = new ResourceTypeBSO();
        CountrySiteBSO countrySiteDb = new CountrySiteBSO();

        public ActionResult Index()
        {
            ViewBag.ResourceTypes = typeDb.GetAll().Select(c => new { c.Id, c.TypeName }).AsEnumerable();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        [GridAction]
        public ActionResult EditCountrySiteResource(int id)
        {
            CountrySiteResource countrySiteResource = db.Find(p => p.Id == id);
            TryUpdateModel(countrySiteResource);
            db.Update(countrySiteResource);
            var d = new GridModel
            {
                Data = db.GetAll().Where(c=>c.CountrySiteId == PersistData.CurrentSite.Id).Select(c => new { c.Id, c.CountrySiteId, c.OrderNo, c.TypeId, c.Title, c.Path, c.IsActive, c.ThumbnailPath, c.Description })
            };
            return View(d);
        }


        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        [GridAction]
        public ActionResult InsertCountrySiteResource()
        {
            //Create a new instance of the EditableProduct class.
            CountrySiteResource countrySiteResource = new CountrySiteResource();
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(countrySiteResource))
            {
                //The model is valid - insert the product.
                db.Insert(countrySiteResource);
            }
            //Rebind the grid
            var d = new GridModel
            {
                Data = db.GetAll().Where(c => c.CountrySiteId == PersistData.CurrentSite.Id).Select(c => new { c.Id, c.CountrySiteId, c.OrderNo, c.TypeId, c.Title, c.Path, c.IsActive, c.ThumbnailPath, c.Description })
            };
            return View(d);
        }

        [GridAction]
        public ActionResult GetCountrySiteResourceList()
        {
            if (db.CountOfRecord(c => c.CountrySiteId == PersistData.CurrentSite.Id) == 0)
            {
                CopyForCountrySite(PersistData.CurrentSite.Id);
            }

            var d = new GridModel
            {
                Data = db.GetAll().Where(c => c.CountrySiteId == PersistData.CurrentSite.Id).Select(c => new { c.Id, c.CountrySiteId, c.OrderNo, c.TypeId, c.Title, c.Path, c.IsActive, c.ThumbnailPath, c.Description })
            };
            return View(d);
        }

        private void CopyForCountrySite(int id)
        {
            var values = db.GetAll(c => c.CountrySiteId == 14);
            foreach (var item in values)
            {
                CountrySiteResource countrySiteResource = new CountrySiteResource
                {
                    CountrySiteId = id,
                    Description = item.Description,
                    IsActive = item.IsActive,
                    OrderNo = item.OrderNo,
                    Path = item.Path,
                    ThumbnailPath = item.ThumbnailPath,
                    Title = item.Title,
                    TypeId = item.TypeId,
                };

                using (CountrySiteResourceBSO insertDb = new CountrySiteResourceBSO())
                {
                    insertDb.Insert(countrySiteResource);
                }
            }
        }

        [GridAction]
        public ActionResult GetResourceType()
        {

            return View(new GridModel(typeDb.GetAll().Select(c => new { c.Id, c.TypeName })));
        }

        
    }
}
